package eu.fishcode.android.ui.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import eu.fishcode.android.utils.Utils;

public class ToolBar extends JToolBar implements MouseListener {

	protected ActionListener actionListener;
	private final int intOrientation;
	private static final double THETA = Math.toRadians(90.0);
	private boolean collapsed;
	private int oldSize;
	private Color headerColor;

	public ToolBar(String name, ActionListener ai, int orientation) {
		super(name);
		this.actionListener = ai;
		this.intOrientation = orientation;
		this.setOrientation(orientation);
		this.setFloatable(true);
		this.setRollover(true);
		this.setBorderPainted(false);
		this.addButtons();
		this.collapsed = false;
		this.addMouseListener(this);
		this.headerColor = new Color(0x00, 0x66, 0xff, 120);
		this.oldSize = 0;
	}

	public void addButton(String imageName, String actionCommand) {
		this.addButton(imageName, actionCommand, this);
	}

	public JButton addButton(String imageName, String actionCommand,
			JComponent panel) {
		final JButton button = new JButton(Utils.getLabel("toolbar."
				+ actionCommand));
		button.setActionCommand(actionCommand);
		button.addActionListener(this.actionListener);
		button.setIcon(Utils.getIcon(imageName));
		button.setToolTipText(button.getText());
		panel.add(button);
		return button;
	}

	protected void addButtons() {
		if (this.intOrientation == SwingConstants.HORIZONTAL) {
			this.add(Box.createRigidArea(new Dimension(5, this.getHeight())));
		} else {
			this.add(Box.createRigidArea(new Dimension(this.getWidth(), 5)));
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		this.headerColor = new Color(0x00, 0x66, 0xff, 220);
		this.repaint();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		this.headerColor = new Color(0x00, 0x66, 0xff, 120);
		this.repaint();
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (this.oldSize == 0) {
			this.oldSize = this.intOrientation == SwingConstants.HORIZONTAL ? this
					.getWidth() : this.getHeight();
		}

		if (this.intOrientation == SwingConstants.HORIZONTAL) {
			if ((e.getX() < 12) && (e.getY() < 10)) {
				if (this.collapsed) {
					this.setPreferredSize(new Dimension(this.oldSize, this
							.getHeight()));
					this.collapsed = false;
				} else {
					this.setPreferredSize(new Dimension(14, this.getHeight()));
					this.collapsed = true;
				}
			}
		} else {
			if ((e.getX() > (this.getWidth() - 10)) && (e.getY() < 12)) {
				if (this.collapsed) {
					this.setPreferredSize(new Dimension(this.getWidth(),
							this.oldSize));
					this.collapsed = false;
				} else {
					this.setPreferredSize(new Dimension(this.getWidth(), 14));
					this.collapsed = true;

				}
			}
		}
		this.revalidate();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void paintComponent(Graphics g) {
		final Graphics2D g2 = (Graphics2D) g.create();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setPaint(new GradientPaint(0, 0, new Color(0xcf, 0xde, 0xf0), 0,
				this.getHeight(), new Color(0x86, 0xab, 0xd9)));
		g2.fillRect(0, 0, this.getWidth(), this.getHeight());

		g2.setPaint(this.headerColor);

		g2.setFont(Utils.ubuntuFont.deriveFont(10f));
		if (this.intOrientation == SwingConstants.HORIZONTAL) {
			g2.fill(new RoundRectangle2D.Double(1, 1, 12, this.getHeight() - 2,
					4, 4));
			g2.rotate(-THETA);
			g2.setColor(new Color(0xcf, 0xde, 0xf0));
			g2.drawString(this.getName(), -this.getHeight() + 4, 10);
			g2.setColor(new Color(0xee, 0x22, 0x22));
			g2.drawString("x", -8, 10);
			g2.rotate(THETA);
		} else {
			g2.fill(new RoundRectangle2D.Double(1, 1, this.getWidth() - 2, 12,
					4, 4));
			g2.setColor(new Color(0xcf, 0xde, 0xf0));
			g2.drawString(this.getName(), 4, 10);
			g2.setColor(new Color(0xee, 0x22, 0x22));
			g2.drawString("x", this.getWidth() - 8, 10);
		}

		g2.dispose();
	}

	@Override
	public void setOrientation(int o) {
		super.setOrientation(this.intOrientation);
	}

}
